/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ik.pbs.Manejadores;

import com.ik.pbs.Encapsulados.Categorias;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Malkavian
 */
public class ManejadorCategorias extends Manejador {

    public static ManejadorCategorias manager = null;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD");
    private SimpleDateFormat shf = new SimpleDateFormat("HH:mm:ss");

    public static ManejadorCategorias getInstance() {
        if (manager == null) {
            manager = new ManejadorCategorias();
        }

        return manager;
    }

    public void ManejadorCategorias() {
    }

    public ArrayList<Categorias> getCategorias(Connection con) {
        ArrayList<Categorias> arregloCategorias = new ArrayList();

        ResultSet rd = null;
        PreparedStatement state;

        String query = "select * from categorias order by id";

        try {
            state = con.prepareCall(query);
            rd = state.executeQuery();

            while (rd.next()) {
                Categorias categoria = new Categorias();
                categoria.setId(rd.getInt("id"));
                categoria.setDescripcion(rd.getString("descripcion"));

                arregloCategorias.add(categoria);
            }

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Error al tomar las categorias!", "Error",
                    JOptionPane.WARNING_MESSAGE);
        }

        return arregloCategorias;
    }

    public Categorias getCategoria(Connection con, int codigo) {
        Categorias categoria = new Categorias();

        ResultSet rd = null;
        PreparedStatement state;

        String query = "select * from categorias where id = ?";

        try {
            state = con.prepareCall(query);
            state.setInt(1, codigo);
            rd = state.executeQuery();

            while (rd.next()) {

                categoria.setId(rd.getInt("id"));
                categoria.setDescripcion(rd.getString("descripcion"));
            }

        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }

        return categoria;
    }

    public boolean insertarCategoria(Connection con, Categorias categoria) {
        PreparedStatement state;

        String query = "insert into categorias values(?,?)";
        try {


            state = con.prepareStatement(query);
            state.setInt(1, categoria.getId());
            state.setString(2, categoria.getDescripcion());

            state.execute();
            return true;
        } catch (SQLException ex) {
            return actualizarCategoria(con, categoria);
        }


    }

    public boolean actualizarCategoria(Connection con, Categorias categoria) {
        PreparedStatement state;

        String query = "update categorias set descripcion = ? where id = ?";

        try {
            state = con.prepareStatement(query);
            state.setInt(2, categoria.getId());
            state.setString(1, categoria.getDescripcion());

            state.execute();
            return true;

        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
            return false;
        }


    }

    public int getNext(Connection con) {
        try {
            ResultSet rd = null;
            PreparedStatement state;
            String query = "select max(id) as max from categorias";

            state = con.prepareStatement(query);
            rd = state.executeQuery();

            if (!rd.next()) {
                return 1;
            } else {
                return (rd.getInt("max") + 1);
            }
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return 0;
    }
    
    
}
